//
//  PLFileManager.m
//  PlatformCommon
//
//  Created by KimmanYip on 2019/3/26.
//  Copyright © 2019 KimmanYip. All rights reserved.
//

#import "PLFileManager.h"
#import "NSBundle+PLPodAssociated.h"

@implementation PLFileManager

#pragma mark - 常用

+ (NSString *)stringOfJson:(NSString *)filename keyword:(NSString *)keyword {
    return [[PLFileManager dictionaryOfJson:filename] objectForKey:keyword];
}

+ (NSString *)stringOfJson:(NSString *)filename keyword:(NSString *)keyword pod:(NSString *)pod {
    return [[PLFileManager dictionaryOfJson:filename pod:pod] objectForKey:keyword];
}

#pragma mark - PLIST
+ (NSDictionary *)dictionaryOfPlist:(NSString *)filename {
    return [self dictionaryOfPlist:filename pod:nil];
}

+ (NSDictionary *)dictionaryOfPlist:(NSString *)filename pod:(NSString *)pod {
    NSBundle *bundle = (pod == nil || pod.length ==0) ? [NSBundle mainBundle] : [NSBundle pl_bundleWithBundleName:nil podName:pod];
    NSString *filePath = [bundle pathForResource:filename ofType:@"plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:filePath]){
        NSDictionary *dict= [[NSDictionary alloc] initWithContentsOfFile:filePath];
        return dict;
    }
    return nil;
}


+ (NSArray *)arrayOfPlist:(NSString *)filename {
    return [self arrayOfPlist:filename pod:nil];
}

+ (NSArray *)arrayOfPlist:(NSString *)filename pod:(NSString *)pod {
    
    NSBundle *bundle = (pod == nil || pod.length ==0) ? [NSBundle mainBundle] : [NSBundle pl_bundleWithBundleName:nil podName:pod];
    NSString *filePath = [bundle pathForResource:filename ofType:@"plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:filePath]){
        NSArray *array= [[NSArray alloc] initWithContentsOfFile:filePath];
        return array;
    }
    return nil;
}

#pragma mark - JSON
+ (NSDictionary *)dictionaryOfJson:(NSString *)filename {
    NSData *jsonData = [PLFileManager dataFromOfFile:filename type:@"json"];
    if (jsonData) {
        id JsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
        if(JsonObject && [JsonObject isKindOfClass:[NSDictionary class]]){
            NSDictionary *mainDict = (NSDictionary*)JsonObject;
            return mainDict;
        }
    }
    return nil;
}

+ (NSDictionary *)dictionaryOfJson:(NSString *)filename pod:(NSString *)pod {
    NSData *jsonData = [PLFileManager dataFromOfFile:filename type:@"json" pod:pod];
    if (jsonData) {
        id JsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
        if(JsonObject && [JsonObject isKindOfClass:[NSDictionary class]]){
            NSDictionary *mainDict = (NSDictionary*)JsonObject;
            return mainDict;
        }
    }
    return nil;
}

+ (NSArray *)arrayOfJson:(NSString *)filename keyword:(NSString *)keyword {
    NSDictionary *dict = [PLFileManager dictionaryOfJson:filename];
    if (dict) {
        return [dict objectForKey:keyword];
    }
    return nil;
}

+ (NSArray *)arrayOfJson:(NSString *)filename keyword:(NSString *)keyword pod:(NSString *)pod {
    NSDictionary *dict = [PLFileManager dictionaryOfJson:filename pod:pod];
    if (dict) {
        return [dict objectForKey:keyword];
    }
    return nil;
}

#pragma mark - NSData
+ (NSData *)dataFromOfFile:(NSString *)filename type:(NSString *)type {
    return [self dataFromOfFile:filename type:type pod:nil];
}

+ (NSData *)dataFromOfFile:(NSString *)filename type:(NSString *)type pod:(NSString *)pod {
    NSBundle *bundle = (pod == nil || pod.length ==0) ? [NSBundle mainBundle] : [NSBundle pl_bundleWithBundleName:nil podName:pod];
    NSString *filePath = [bundle pathForResource:filename ofType:type];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
        return data;
    } else {
        return nil;
    }
}

@end
